package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.*;
import com.abaaba.project2.bean.bo.*;
import com.abaaba.project2.bean.data.CouponAndUserInfo;
import com.abaaba.project2.bean.data.CouponInfo;
import com.abaaba.project2.bean.vo.*;
import com.abaaba.project2.mapper.CouponMapper;
import com.abaaba.project2.mapper.Coupon_UserMapper;
import com.abaaba.project2.service.CouponService;
import com.abaaba.project2.util.DateToStringUtil;
import com.abaaba.project2.util.GetExchangeCode;
import com.abaaba.project2.util.PageHelperUtil;
import com.abaaba.project2.util.StringToDate;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.System;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    CouponMapper couponMapper;

    @Autowired
    Coupon_UserMapper coupon_userMapper;

    @Override
    public CouponListVo queryCoupon(CouponReqBo baseReqBo) {

        //这里做判断：根据是否包含name、type、status来判断查询方式
        String name = baseReqBo.getName();
        Short type = baseReqBo.getType();
        Short status = baseReqBo.getStatus();

        CouponListVo couponListVo = null;

        if (name == null && type == null && status == null) {
            couponListVo = queryAllcoupon(baseReqBo);
        } else {
            couponListVo = queryCouponByNameAndTypeAndStatus(baseReqBo);
        }
        return couponListVo;
    }

    @Override
    public AddCouponVo createCoupon(AddCouponReqBo addCouponReqBo) throws ParseException {
        //直接插入即可,注意：这里要根据timeType来判断优惠券的有效期基于什么方式
        //如果生成的是兑换券即type=2，这里要注意，要给该类型的券随机生成一个兑换码
        //timetype=0有效期表示基于days，timetype=1表示有效时间基于startTime、endTime
        //创建example对象
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();

        Coupon coupon = new Coupon();

        if (addCouponReqBo.getType() == 2) {
            String code = GetExchangeCode.genRandomNum();
            coupon.setCode(code);
        }

        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        coupon.setAddTime(date);
        coupon.setUpdateTime(date);
        //String-->short
        //String days = addCouponReqBo.getDays();
        Short days = addCouponReqBo.getDays();
        coupon.setDays(Short.valueOf(days));
        coupon.setDesc(addCouponReqBo.getDesc());
        //String-->BigDemical
        //String discount = addCouponReqBo.getDiscount();
        BigDecimal discount = addCouponReqBo.getDiscount();
        coupon.setDiscount(discount);

        //String-->Date，有效时间基于statTime、endTime
        //利用工具类
        if (addCouponReqBo.getTimeType() == 1) {
            Date startTime = StringToDate.getDate(addCouponReqBo.getStartTime());
            coupon.setStartTime(startTime);
            Date endTime = StringToDate.getDate(addCouponReqBo.getEndTime());
            coupon.setEndTime(endTime);
        }
        coupon.setGoodsType(addCouponReqBo.getGoodsType());
        coupon.setGoodsValue(addCouponReqBo.getGoodsValue());
        coupon.setLimit(addCouponReqBo.getLimit());

        //String-->BigDemical
        //String min = addCouponReqBo.getMin();
        BigDecimal min = addCouponReqBo.getMin();
        coupon.setMin(min);
        coupon.setName(addCouponReqBo.getName());

        coupon.setStatus(addCouponReqBo.getStatus());
        coupon.setTag(addCouponReqBo.getTag());
        coupon.setTimeType(addCouponReqBo.getTimeType());
        coupon.setTotal(addCouponReqBo.getTotal());
        coupon.setType(addCouponReqBo.getType());

        //将记录插入到数据库中（由于要返回主键id，这里需要对xml进行修改）
        couponMapper.insertSelective(coupon);

        AddCouponVo addCouponVo = new AddCouponVo();
        addCouponVo.setCode(coupon.getCode());
        addCouponVo.setAddTime(DateToStringUtil.dateToString(coupon.getAddTime()));
        addCouponVo.setDays(coupon.getDays());
        addCouponVo.setDesc(coupon.getDesc());
        addCouponVo.setDiscount(coupon.getDiscount());
        addCouponVo.setGoodsType(coupon.getGoodsType());
        addCouponVo.setGoodsValue(coupon.getGoodsValue());
        addCouponVo.setId(coupon.getId());
        addCouponVo.setLimit(coupon.getLimit());
        addCouponVo.setMin(coupon.getMin());
        addCouponVo.setName(coupon.getName());
        addCouponVo.setStatus(coupon.getStatus());
        addCouponVo.setTag(coupon.getTag());
        addCouponVo.setTimeType(coupon.getTimeType());
        addCouponVo.setTotal(coupon.getTotal());
        addCouponVo.setType(coupon.getType());
        addCouponVo.setUpdateTime(DateToStringUtil.dateToString(coupon.getUpdateTime()));
//        addCouponVo.setStartTime(DateToStringUtil.dateToString(coupon.getStartTime()));
//        addCouponVo.setEndTime(DateToStringUtil.dateToString(coupon.getEndTime()));

        return addCouponVo;
    }

    @Override
    public UpdateCouponVo updateCoupon(UpdateCouponBo updateCouponBo) throws ParseException {

        //更新条件：根据id更新即可
        //创建example对象
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();
        Coupon coupon = new Coupon();
        Date date = new Date(System.currentTimeMillis());
        coupon.setUpdateTime(date);
        coupon.setId(updateCouponBo.getId());
        coupon.setName(updateCouponBo.getName());
        coupon.setDesc(updateCouponBo.getDesc());
        coupon.setTag(updateCouponBo.getTag());
        coupon.setMin(updateCouponBo.getMin());
        coupon.setDiscount(updateCouponBo.getDiscount());
        coupon.setLimit(updateCouponBo.getLimit());
        coupon.setType(updateCouponBo.getType());
        coupon.setTotal(updateCouponBo.getTotal());
        coupon.setDays(updateCouponBo.getDays());

        //String-->Date，有效时间基于statTime、endTime
        //利用工具类
        if (updateCouponBo.getTimeType() == 1) {
            Date startTime = StringToDate.getDate(updateCouponBo.getStartTime());
            coupon.setStartTime(startTime);
            Date endTime = StringToDate.getDate(updateCouponBo.getEndTime());
            coupon.setEndTime(endTime);
        }

        couponMapper.updateByPrimaryKeySelective(coupon);

        UpdateCouponVo updateCouponVo = new UpdateCouponVo();
        //Date-->String
        updateCouponVo.setAddTime(updateCouponBo.getAddTime());
        updateCouponVo.setDays(coupon.getDays());
        updateCouponVo.setDesc(coupon.getDesc());
        //BigDecimal-->int
        updateCouponVo.setDiscount(Integer.valueOf(coupon.getDiscount().toString()));
        updateCouponVo.setGoodsType(updateCouponBo.getGoodsType());
        updateCouponVo.setGoodsValue(updateCouponBo.getGoodsValue());
        updateCouponVo.setId(coupon.getId());
        updateCouponVo.setLimit(coupon.getLimit());
        updateCouponVo.setMin(Integer.valueOf(coupon.getMin().toString()));
        updateCouponVo.setName(coupon.getName());
        updateCouponVo.setStatus(updateCouponBo.getStatus());
        updateCouponVo.setTag(coupon.getTag());
        updateCouponVo.setTimeType(updateCouponBo.getTimeType());
        updateCouponVo.setTotal(coupon.getTotal());
        updateCouponBo.setType(coupon.getType());
        updateCouponVo.setUpdateTime(updateCouponBo.getUpdateTime());

        return updateCouponVo;
    }

    @Override
    public void deleteCoupon(UpdateCouponBo updateCouponBo) {

        //逻辑删除即可：deleted = 1
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();
        //拼接sql语句
        criteria.andIdEqualTo(updateCouponBo.getId());
        Coupon coupon = new Coupon();
        coupon.setId(updateCouponBo.getId());
        coupon.setDeleted(true);
        couponMapper.updateByPrimaryKeySelective(coupon);

    }

    @Override
    public CouponDetailVo queryCouponById(Integer id) {

        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();
        criteria.andIdEqualTo(id);

        Coupon coupon = couponMapper.selectByPrimaryKey(id);

        CouponDetailVo couponDetailVo = new CouponDetailVo();

        couponDetailVo.setId(coupon.getId());
        couponDetailVo.setName(coupon.getName());
        couponDetailVo.setDesc(coupon.getDesc());
        couponDetailVo.setTag(coupon.getTag());
        couponDetailVo.setTotal(coupon.getTotal());
        //BigDemical
        couponDetailVo.setDiscount(coupon.getDiscount());
        couponDetailVo.setMin(coupon.getMin());
        couponDetailVo.setLimit(coupon.getLimit());
        couponDetailVo.setType(coupon.getType());
        couponDetailVo.setStatus(coupon.getStatus());
        couponDetailVo.setGoodsType(coupon.getGoodsType());
        couponDetailVo.setCode(coupon.getCode());
        couponDetailVo.setTimeType(coupon.getTimeType());

        //这里注意：timeType基于什么方式：0表示基于days，1表示基于startTime、endTime
        if (coupon.getTimeType() == 0) {
            couponDetailVo.setDays(coupon.getDays());
        }

        if (coupon.getTimeType() == 1) {
            couponDetailVo.setStartTime(DateToStringUtil.dateToString(coupon.getStartTime()));
            couponDetailVo.setEndTime(DateToStringUtil.dateToString(coupon.getEndTime()));
        }

        couponDetailVo.setAddTime(DateToStringUtil.dateToString(coupon.getAddTime()));
        couponDetailVo.setUpdateTime(DateToStringUtil.dateToString(coupon.getUpdateTime()));
        couponDetailVo.setDeleted(coupon.getDeleted());
        couponDetailVo.setGoodsValue(coupon.getGoodsValue());

        return couponDetailVo;
    }

    @Override
    public CouponAndUserVo queryCouponAndUser(CouponAndUserReqBo couponAndUserReqBo) {
        //1.显示所有记录 2.根据条件进行查询：用户id和使用状态
        //这里的显示要注意：考虑
        Integer userId = couponAndUserReqBo.getUserId();
        Short status = couponAndUserReqBo.getStatus();

        CouponAndUserVo couponAndUserVo = null;
        if (userId == null && status == null) {
            couponAndUserVo = queryAll(couponAndUserReqBo);
        } else {
            couponAndUserVo = queryByUserIdAndStatus(couponAndUserReqBo);
        }

        return couponAndUserVo;
    }

    private CouponAndUserVo queryByUserIdAndStatus(CouponAndUserReqBo couponAndUserReqBo) {
        //使用工具类进行分页查询
        PageHelperUtil.startPage(couponAndUserReqBo);
        //查询条件：根据id、status、couponId、deleted = 0
        //创建example对象
        Coupon_UserExample coupon_userExample = new Coupon_UserExample();
        Coupon_UserExample.Criteria criteria = coupon_userExample.createCriteria();

        Integer userId = couponAndUserReqBo.getUserId();
        Short status = couponAndUserReqBo.getStatus();

        //排序信息
        coupon_userExample.setOrderByClause(couponAndUserReqBo.getSort() + " " + couponAndUserReqBo.getOrder());

        //拼接sql语句
        criteria.andCouponIdEqualTo(couponAndUserReqBo.getCouponId());
        criteria.andDeletedEqualTo(false);
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        List<Coupon_User> coupon_users = coupon_userMapper.selectByExample(coupon_userExample);
        //获得分页信息
        PageInfo<Coupon_User> coupon_userPageInfo = new PageInfo<>(coupon_users);
        long total = coupon_userPageInfo.getTotal();

        CouponAndUserVo couponAndUserVo = new CouponAndUserVo();
        couponAndUserVo.setTotal(total);
        ArrayList<CouponAndUserInfo> couponAndUserInfos = new ArrayList<>();
        for (Coupon_User coupon_user : coupon_users) {
            CouponAndUserInfo couponAndUserInfo = new CouponAndUserInfo();
            couponAndUserInfo.setAddTime(DateToStringUtil.dateToString(coupon_user.getAddTime()));
            couponAndUserInfo.setUpdateTime(DateToStringUtil.dateToString(coupon_user.getUpdateTime()));
            couponAndUserInfo.setCouponId(coupon_user.getCouponId());
            couponAndUserInfo.setDeleted(coupon_user.getDeleted());
            couponAndUserInfo.setId(coupon_user.getId());
            couponAndUserInfo.setUserId(coupon_user.getUserId());
            couponAndUserInfo.setOrderId(coupon_user.getOrderId());
            couponAndUserInfo.setStatus(coupon_user.getStatus());
            if (coupon_user.getUsedTime() != null) {
                couponAndUserInfo.setUserdTime(DateToStringUtil.dateToString(coupon_user.getUsedTime()));
            }
            if (coupon_user.getStartTime() != null) {
                couponAndUserInfo.setStartTime(DateToStringUtil.dateToString(coupon_user.getStartTime()));
            }
            if (coupon_user.getEndTime() != null) {
                couponAndUserInfo.setEndTime(DateToStringUtil.dateToString(coupon_user.getEndTime()));
            }
            couponAndUserInfos.add(couponAndUserInfo);
        }
        couponAndUserVo.setItems(couponAndUserInfos);
        return couponAndUserVo;
    }

    private CouponAndUserVo queryAll(CouponAndUserReqBo couponAndUserReqBo) {
        //使用工具类进行分页查询
        PageHelperUtil.startPage(couponAndUserReqBo);

        //查询条件：couponId，deleted = 0
        //创建example对象
        Coupon_UserExample coupon_userExample = new Coupon_UserExample();
        Coupon_UserExample.Criteria criteria = coupon_userExample.createCriteria();

        //排序信息
        coupon_userExample.setOrderByClause(couponAndUserReqBo.getSort() + " " + couponAndUserReqBo.getOrder());

        //拼接sql语句
        criteria.andCouponIdEqualTo(couponAndUserReqBo.getCouponId());
        criteria.andDeletedEqualTo(false);
        List<Coupon_User> coupon_users = coupon_userMapper.selectByExample(coupon_userExample);

        //获得分页信息
        PageInfo<Coupon_User> coupon_userPageInfo = new PageInfo<>(coupon_users);
        long total = coupon_userPageInfo.getTotal();

        CouponAndUserVo couponAndUserVo = new CouponAndUserVo();
        couponAndUserVo.setTotal(total);
        ArrayList<CouponAndUserInfo> couponAndUserInfos = new ArrayList<>();
        for (Coupon_User coupon_user : coupon_users) {
            CouponAndUserInfo couponAndUserInfo = new CouponAndUserInfo();
            couponAndUserInfo.setAddTime(DateToStringUtil.dateToString(coupon_user.getAddTime()));
            couponAndUserInfo.setUpdateTime(DateToStringUtil.dateToString(coupon_user.getUpdateTime()));
            couponAndUserInfo.setCouponId(coupon_user.getCouponId());
            couponAndUserInfo.setDeleted(coupon_user.getDeleted());
            couponAndUserInfo.setId(coupon_user.getId());
            couponAndUserInfo.setUserId(coupon_user.getUserId());
            couponAndUserInfo.setOrderId(coupon_user.getOrderId());
            couponAndUserInfo.setStatus(coupon_user.getStatus());
            if (coupon_user.getUsedTime() != null) {
                couponAndUserInfo.setUserdTime(DateToStringUtil.dateToString(coupon_user.getUsedTime()));
            }
            if (coupon_user.getStartTime() != null) {
                couponAndUserInfo.setStartTime(DateToStringUtil.dateToString(coupon_user.getStartTime()));
            }
            if (coupon_user.getEndTime() != null) {
                couponAndUserInfo.setEndTime(DateToStringUtil.dateToString(coupon_user.getEndTime()));
            }
            couponAndUserInfos.add(couponAndUserInfo);
        }
        couponAndUserVo.setItems(couponAndUserInfos);
        return couponAndUserVo;
    }

    private CouponListVo queryCouponByNameAndTypeAndStatus(CouponReqBo baseReqBo) {
        //使用工具类进行分页查询
        PageHelperUtil.startPage(baseReqBo);

        //查询条件：deleted = 0，根据name、type、status进行模糊查询
        //创建example对象
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();

        String name = baseReqBo.getName();
        Short type = baseReqBo.getType();
        Short status = baseReqBo.getStatus();

        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }

        //排序信息
        couponExample.setOrderByClause(baseReqBo.getSort() + " " + baseReqBo.getOrder());

        //拼接sql语句
        criteria.andDeletedEqualTo(false);
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);

        //获得分页信息
        PageInfo<Coupon> couponPageInfo = new PageInfo<>(coupons);
        long total = couponPageInfo.getTotal();

        CouponListVo couponListVo = new CouponListVo();
        couponListVo.setTotal(total);

        ArrayList<CouponInfo> couponInfos = new ArrayList<>();
        for (Coupon coupon : coupons) {
            CouponInfo couponInfo = new CouponInfo();
            couponInfo.setId(coupon.getId());
            couponInfo.setName(coupon.getName());
            couponInfo.setDesc(coupon.getDesc());
            couponInfo.setTag(coupon.getTag());
            couponInfo.setTotal(coupon.getTotal());
            couponInfo.setDiscount(coupon.getDiscount());
            couponInfo.setMin(coupon.getMin());
            couponInfo.setLimit(coupon.getLimit());
            couponInfo.setType(coupon.getType());
            couponInfo.setStatus(coupon.getStatus());
            couponInfo.setGoodsType(coupon.getGoodsType());
            couponInfo.setGoodsValue(coupon.getGoodsValue());
            couponInfo.setTimeType(coupon.getTimeType());
            couponInfo.setDays(coupon.getDays());
            if (coupon.getTimeType() == 1) {
                couponInfo.setStartTime(DateToStringUtil.dateToString(coupon.getStartTime()));
                couponInfo.setEndTime(DateToStringUtil.dateToString(coupon.getEndTime()));
            }
            couponInfo.setAddTime(DateToStringUtil.dateToString(coupon.getAddTime()));
            couponInfo.setUpdateTime(DateToStringUtil.dateToString(coupon.getUpdateTime()));
            couponInfo.setDeleted(coupon.getDeleted());
            couponInfos.add(couponInfo);
        }
        couponListVo.setItems(couponInfos);
        return couponListVo;
    }

    private CouponListVo queryAllcoupon(CouponReqBo baseReqBo) {

        //使用工具类进行分页查询
        PageHelperUtil.startPage(baseReqBo);

        //查询条件：deleted = 0
        //创建example对象
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();

        //排序信息
        couponExample.setOrderByClause(baseReqBo.getSort() + " " + baseReqBo.getOrder());

        //拼接sql语句
        criteria.andDeletedEqualTo(false);
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);

        //获得分页信息
        PageInfo<Coupon> couponPageInfo = new PageInfo<>(coupons);
        long total = couponPageInfo.getTotal();

        CouponListVo couponListVo = new CouponListVo();
        couponListVo.setTotal(total);

        ArrayList<CouponInfo> couponInfos = new ArrayList<>();
        for (Coupon coupon : coupons) {
            CouponInfo couponInfo = new CouponInfo();
            couponInfo.setId(coupon.getId());
            couponInfo.setName(coupon.getName());
            couponInfo.setDesc(coupon.getDesc());
            couponInfo.setTag(coupon.getTag());
            couponInfo.setTotal(coupon.getTotal());
            couponInfo.setDiscount(coupon.getDiscount());
            couponInfo.setMin(coupon.getMin());
            couponInfo.setLimit(coupon.getLimit());
            couponInfo.setType(coupon.getType());
            couponInfo.setStatus(coupon.getStatus());
            couponInfo.setGoodsType(coupon.getGoodsType());
            couponInfo.setGoodsValue(coupon.getGoodsValue());
            couponInfo.setTimeType(coupon.getTimeType());
            couponInfo.setDays(coupon.getDays());
            if (coupon.getTimeType() == 1) {
                couponInfo.setStartTime(DateToStringUtil.dateToString(coupon.getStartTime()));
                couponInfo.setEndTime(DateToStringUtil.dateToString(coupon.getEndTime()));
            }
            couponInfo.setAddTime(DateToStringUtil.dateToString(coupon.getAddTime()));
            couponInfo.setUpdateTime(DateToStringUtil.dateToString(coupon.getUpdateTime()));
            couponInfo.setDeleted(coupon.getDeleted());
            couponInfos.add(couponInfo);
        }
        couponListVo.setItems(couponInfos);
        return couponListVo;
    }
}
